package com.example.common.vp;

import android.content.Context;

import com.example.common.vp.interfaces.IDisposablePool;
import com.example.common.vp.interfaces.IPresenter;
import com.example.common.vp.interfaces.IView;

import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;

public abstract class BasePresenter<V extends IView> implements IPresenter<V>, IDisposablePool {

    protected V mView;
    private CompositeDisposable compositeDisposable;

    @Override
    public void attachView(V view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        clearPool();
        mView = null;
    }

    @Override
    public void addDisposable(Disposable disposable) {
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.add(disposable);
    }

    @Override
    public Context getContext() {
        return mView.getContext();
    }

    @Override
    public void clearPool() {
        if (compositeDisposable != null) {
            compositeDisposable.clear();
            compositeDisposable = null;
        }
    }
}
